package com.dealer.management.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dealer.management.entity.Product;
import com.dealer.management.mapper.ProductMapper;
import com.dealer.management.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public IPage<Product> getProductPage(Page<Product> page, String name, Long categoryId) {
        return productMapper.selectProductPage(page, name, categoryId);
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public boolean saveProduct(Product product) {
        return productMapper.insert(product) > 0;
    }

    @Override
    public boolean updateProduct(Product product) {
        return productMapper.updateById(product) > 0;
    }

    @Override
    public boolean deleteProduct(Long id) {
        return productMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateStock(Long id, Integer quantity) {
        Product product = productMapper.selectById(id);
        if (product != null) {
            product.setStockQuantity(quantity);
            return productMapper.updateById(product) > 0;
        }
        return false;
    }
}
